package com.itheima.interceptor;

import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取token
        String jwt = request.getHeader("token");
        if (jwt == null) {
            response.setStatus(401);
            return false;
        }

        // TODO: 验证token
        try {
            JwtUtils.parseJWT(jwt);
            return true;
        } catch (Exception e) {
            response.setStatus(401);
            return false;
        }

    }
}
